<!DOCTYPE html>
<html>
<head>
<#include "/common/freemarker/include_css.ftl">
<title>配置常用用户</title>
</head>
<body>
	<title>常用用户设置</title>
	<body>
		<div class="div">		
			<table border="0"  align="center" cellpadding="0" cellspacing="1" width="99%">
				<tr class="content_tab_input">
					<td colspan="3" align="left">
					<input type="radio" name="queryType" style="border:0px" id="systemType" value="3" onClick="loadUsers()"/ >用户列表
					&nbsp;
					<#--
					<input type="radio" name="queryType" style="border:0px" id="projectType" value="1" onClick="findConfUsers(this.value)"/ >项目架构
					&nbsp;
					<input type="radio" name="queryType" style="border:0px" id="departType" value="2" onClick="findConfUsers(this.value)"/ >部门架构
					&nbsp;
					<input type="radio" name="queryType" style="border:0px" id="confType" value="0" onClick="loadUsers()"/>常用用户
					 <a href="javascript:confUsers()">配置</a>
					 -->
					&nbsp;
					<input type="checkbox" name="includeDisabled" style="border:0px" id="includeDisabled" value="1" />包含离职人员
					<input type="hidden" name="projectDepartFlag" value="">
					
					
 
					</td>
				</tr>
			</table>
 
			<table border="0" align="center" cellpadding="0" cellspacing="1" width="99%"  class="content_tab_input">
				
				<tr>
					<td align="left" width="40%">按用户名或拼音字母查找
						<input type="text"    id="loading" name="loading" value="" style="border:0px; background:inherit; color:#FF0000" size="11"></td>
					<td></td>
					<td width="40%" align="center">常用用户</td>
				</tr>
				<tr>
					<td valign="middle" align="left">
						<input type="text" name="userPinYin" onkeypress="selUserEnter();" size="25" style="width:160px">
						<input type="hidden" name="currentId" value="-1">
						<input type="hidden" name="parentId" value="-1">
 
						<input type="hidden" name="nowUserPinYin" value=""><br>
						<select id="ExcelSel" name="ExcelSel" size="17" style="width:240px" ondblclick="loadChild()" multiple>
						</select>
					</td>
					<td valign="middle">
						<table border="0" align="center" width="100%">
							<tr align="center"><td><input type="button" value="添加" class="btn btn-primary btn-xs" onClick="choseInsert()" name=btnAdd></td></tr>
							<tr align="center"><td>&nbsp;</td></tr>
							<tr align="center"><td>&nbsp;</td></tr>
							<tr align="center"><td><input type="button" value="移除" class="btn btn-primary btn-xs" onClick="mydelete()" name=btnDel></td></tr>
						</table>
					</td>
					<td width="35%" valign="middle" align="center"> 
						<select name="ExcelOut" size="18" style="width:240px" ondblclick="mydelete()" multiple>	
						</select>
					</td>
				</tr>
			</table>
			<table width="99%" border="0" align="center" cellpadding="0" cellspacing="1">
				<tr>
					<td align="center">
					    <br>
			 
						<input name="btnOK" type="button" class="btn btn-sm   btn-primary" value="保 存" onclick="AddCheck()">&nbsp;&nbsp;
						<input name="btnBack" type="button" class="btn btn-sm btn-default" value="返 回" onclick="history.go(-1);">
					</td>
				</tr>
			</table>
		</div>
		
	 <#--  公共js -->
	 <#include "/common/freemarker/include_js.ftl">
	  
	 <script language="javascript">
		//得到radio的值     
		function getRadioValue(radioName){
	         var obj=document.getElementsByName(radioName);     
	         for(var i=0;i<obj.length;i++){     
		         if(obj[i].checked){  

		             return obj[i].value;     
		         }     
	         }     
	     } 
		function loadUsers(){
		     //var includeDisabled=$("input[name='includeDisabled'][checked]").val(); 
		     //var queryType=$("input[name='queryType'][checked]").val(); 
		     var includeDisabled=getRadioValue("includeDisabled");
		     var queryType=getRadioValue("queryType"); 
		     var userPinYin=document.all("userPinYin").value;
		     var data={"userPinYin":userPinYin,"includeDisabled":includeDisabled,"queryType":queryType };
		     document.all("ExcelSel").options.length=0;
		 
			 $.ajax({ 
				url: ctx+"common/selectuser/select_user_any_way_doselect.do", 
				type: "GET",
				data: data, 
				dataType:"json",
				success: function(rhs) {
				    var data=rhs["data"];
				    //var data=  eval('(' + rhs + ')');
				    //var data= rhs;
				    var selected=false;
				    for (var i=0;i<data.length;i++){ 
						var username=data[i][2]+"("+data[i][1]+")";
						var userid=data[i][0] ;
						var account=data[i][1] ;
						var nameBack=data[i][2] ;
						var NOPrefix=data[i][3] ;
						document.all("ExcelSel").options[document.all("ExcelSel").length] = new Option(username,userid);
						document.all("ExcelSel").options[document.all("ExcelSel").length-1].account = account;
						document.all("ExcelSel").options[document.all("ExcelSel").length-1].nameBack = nameBack;
						document.all("ExcelSel").options[document.all("ExcelSel").length-1].NOPrefix = NOPrefix;
						if (NOPrefix=="USER" && selected==false){
							document.all("ExcelSel").options[document.all("ExcelSel").length-1].selected = true;  
							selected=true;
						}
					}
					//$("#ExcelSel").html(h);
				} 
			}); 
		}

		function QueryString(sName) {
			 return getpara(sName);
		}
		
		window.onload = function(){
			 $("#systemType").attr("checked",true);
			 
 			loadUsers();
 			
 			initOut();
 			
			if(document.all("userPinYin").style.display!="none"){
				document.all("userPinYin").focus();
			}
			window.setInterval("selUserInterval()",500);
		}
		
		function selUserInterval() {
			var l = 0;

			var searchstr = document.all.userPinYin.value;
			var re = new RegExp(searchstr,"i");
			var userPinyin = document.all("userPinYin").value;
			//判断是否中文,中文大于一个可查询,英文大于两个可查询
			if (searchstr.replace(/ /g,"")!= "" && document.all("nowUserPinYin").value!=searchstr.replace(/ /g,"")) {
				
				if(ischinese(searchstr)&& searchstr.length>0){
					document.all("nowUserPinYin").value=searchstr.replace(/ /g,"");
					loadUsers();
					currType = -1;
				}
				if(!ischinese(searchstr)&& searchstr.length>1){
					document.all("nowUserPinYin").value=searchstr.replace(/ /g,"");
					loadUsers();
					currType = -1;
				}
			}
			if (searchstr.replace(/ /g,"")== "" && document.all("nowUserPinYin").value!=searchstr.replace(/ /g,"")){
				document.all("nowUserPinYin").value=searchstr.replace(/ /g,"");
				loadUsers();
			}
			
			return true;
		}
		
		function ischinese(s){
			var ret=true;
			for(var i=0;i<s.length;i++)
			ret=ret && (s.charCodeAt(i)>=10000);
			return ret;
		} 
		
		function initOut() {
			$.ajax({ 
				url: ctx+"common/selectuser/select_user_any_way_getConfUsers.do", 
				type: "GET",
				data: {}, 
				dataType:"json",
				success: function(rhs) {
				    //alert(rhs);
				    var data=rhs["data"];
				    var selected=false;
				    var selObj=document.all("ExcelOut");
				    for (var i=0;i<data.length;i++){ 
						var username=data[i][2]+"("+data[i][1]+")";
						var userid=data[i][0] ;
						var account=data[i][1] ;
						var nameBack=data[i][2] ;
						var NOPrefix=data[i][3] ;
						selObj.options[selObj.length] = new Option(username,userid);
						selObj.options[selObj.length-1].account = account;
						selObj.options[selObj.length-1].nameBack = nameBack;
						//selObj.options[selObj.length-1].NOPrefix = NOPrefix;
					}
					//$("#ExcelSel").html(h);
				} 
			}); 
            document.all("ExcelSel").multiple=true;
		    document.all("ExcelOut").multiple=true;

	        if(document.all("userPinYin").style.display!="none"){
				document.all("userPinYin").focus();
			}
			 
		}
		
		function selUserEnter() {
			var l = 0;
			var searchstr = document.all.userPinYin.value;
			var re = new RegExp(searchstr,"i");
			var userPinyin = document.all("userPinYin").value;
			//如果是按回车键
			if (window.event.keyCode == 13 ) {
				if (document.all.ExcelSel.selectedIndex!=-1){
					choseInsert();
				}
			}
			
			return true;
		}
		
		   
		
		function choseInsert() {
			myinsertMultiple();
		}
		function myinsert() {
			oindex = document.all.ExcelSel.selectedIndex;
			olength = document.all.ExcelOut.length;
 		 
			//如果该选项标志为用户
			if (document.all.ExcelSel.options[document.all.ExcelSel.selectedIndex].NOPrefix == "USER") {
				
				//如果已选择用户为空
				if (olength == 0) {
					
					//如果选择了用户
					if(oindex >= 0) {
    
						otext = document.all.ExcelSel.options[document.all.ExcelSel.selectedIndex].text;
						onameBack = document.all.ExcelSel.options[document.all.ExcelSel.selectedIndex].nameBack;
						ovalue = document.all.ExcelSel.options[document.all.ExcelSel.selectedIndex].value;
						oaccount = document.all.ExcelSel.options[document.all.ExcelSel.selectedIndex].account;
						
						document.all.ExcelOut.options[0] = new Option(onameBack,0);
						document.all.ExcelOut.options[0].value = ovalue;
						document.all.ExcelOut.options[0].account = oaccount;
						document.all.ExcelSel.selectedIndex = -1;
					}
				}
				else {
					
					//如果选择了用户
					if(oindex >= 0) {
						otext = document.all.ExcelSel.options[document.all.ExcelSel.selectedIndex].text;
						ovalue = document.all.ExcelSel.options[document.all.ExcelSel.selectedIndex].value;
						oaccount = document.all.ExcelSel.options[document.all.ExcelSel.selectedIndex].account;
						onameBack = document.all.ExcelSel.options[document.all.ExcelSel.selectedIndex].nameBack;
						
						//清空已选择用户
						document.all.ExcelOut.length = 0;
						
						document.all.ExcelOut.options[0] = new Option(onameBack,0);
						document.all.ExcelOut.options[0].value = ovalue;
						document.all.ExcelOut.options[0].account = oaccount;
						document.all.ExcelSel.selectedIndex = -1;
					}
				}
			}
		}
		
		function myinsertMultiple() {
			var retarr = new Array(2);
			retarr[0] = "";
			retarr[1] = "";
			
			//循环保存上次已选择用户		  
			for (var oindex = 0; oindex < document.all.ExcelOut.length; oindex++) {
			    if (oindex == 0) {
			        retarr[0] = retarr[0] + "," + document.all.ExcelOut.options[oindex].text;
			        retarr[1] = retarr[1] + "," + document.all.ExcelOut.options[oindex].value;        
			    } else {
			        retarr[0] = retarr[0] + "," + document.all.ExcelOut.options[oindex].text;
			        retarr[1] = retarr[1] + "," + document.all.ExcelOut.options[oindex].value;		        
			    }			    
			}		
			oindex = document.all.ExcelSel.selectedIndex;
			olength = document.all.ExcelOut.length;
			
			//循环设置这次选择的用户
			for(var i=0;i<document.all.ExcelSel.length;i++){
				
				//如果已选中该用户
			    if(document.all.ExcelSel.options[i].selected){
			        otext = document.all.ExcelSel.options[i].text;
				    ovalue = document.all.ExcelSel.options[i].value;
					oaccount = document.all.ExcelSel.options[i].account;
					onameBack = document.all.ExcelSel.options[i].nameBack;
					
					//如果保存数组中找不到选择了的用户就更新到已选框中
					var flag = true;
					var retarrs = retarr[1].split(/,/);
					for(var j=0;j<retarrs.length;j++){
						if (retarrs[j]==ovalue){
							flag=false;
							break;
						}
					}
					

					if (flag){
						//如果该选项标志为用户
						if (document.all.ExcelSel.options[i].NOPrefix == "USER") {
					        document.all.ExcelOut.options[document.all.ExcelOut.length] = new Option(onameBack,ovalue);
					        document.all.ExcelOut.options[document.all.ExcelOut.length-1].account = oaccount;
				        }
				    }    
			    }

			}
			document.all.ExcelSel.selectedIndex = -1;
		}
		
		function mydelete() {
		    for(var i=document.all.ExcelOut.length-1;i>-1;i--){
			    if(document.all.ExcelOut.options[i].selected){
			        document.all.ExcelOut.options[i] = null;
			    }
			}
		} 

		function AddCheck() {
 
			var retarr = new Array(3);
			retarr[0] = "";
			retarr[1] = "";
			retarr[2] = "";
			
			//循环编辑输入的用户名和ID
			for (var oindex = 0; oindex < document.all.ExcelOut.length; oindex++) {
			    if (oindex == 0) {
			        retarr[0] = document.all.ExcelOut.options[oindex].text;
			        retarr[1] = document.all.ExcelOut.options[oindex].value;
			        retarr[2] = document.all.ExcelOut.options[oindex].account;
			    } else {
			        retarr[0] = retarr[0] + "," + document.all.ExcelOut.options[oindex].text;
			        retarr[1] = retarr[1] + "," + document.all.ExcelOut.options[oindex].value;
			        retarr[2] = retarr[2] + "," + document.all.ExcelOut.options[oindex].account;
			    }
			}
			 var data={"selUserIds":retarr[1]};
			 alert(retarr[1])
			 if (retarr[1]==""){
			 	 art.dialog.alert("请选择用户");
			 	 return;
			 }
			 $.ajax({ 
				url: ctx+"common/selectuser/select_user_any_way_submitConfUsers.do", 
				type: "POST",
				data: data, 
				dataType:"json",
				success: function(rhs) {
					 if (rhs.status=="success"){
					     //跳转回用户选择
					      var userId_txtName=QueryString("userId_txtName");
					     var userAccount_txtName=QueryString("userAccount_txtName");
					     var userName_txtName=QueryString("userName_txtName");
					     //var parentWindow = QueryString("parentWindow");
					     var params= "userId_txtName="+userId_txtName+"&userAccount_txtName="+userAccount_txtName+"&userName_txtName="+userName_txtName;//+"&parentWindow="+parentWindow
					     window.location=ctx+"common/selectuser/select_user_any_way.do?"+params;
					     art.dialog.data("queryType","confType");
				     }else{
					     art.dialog.alert(rhs.message);
				     }
				} 
			}); 
			
			
		}
 	  	function loadChild(){
  			var NOPrefix = document.all.ExcelSel.options[document.all.ExcelSel.selectedIndex].NOPrefix;
  			if (NOPrefix!=null && NOPrefix=="USER"){
  				choseInsert();
  			}
	  	}
	</script>	
	</body>
</html>